package cn.edu.nju.software.android.layoutHelper;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.nju.software.android.activity.PostActivity;
import cn.edu.nju.software.android.activity.R;
import com.weibo.sdk.android.keep.AccessTokenKeeper;
import gongshw.android.mypm.UpdateTask;
import gongshw.android.mypm.factory.ServiceFactory;
import gongshw.android.mypm.model.CityItem;
import gongshw.android.mypm.service.LocationService;
import gongshw.android.mypm.service.PmService;

public class SettingsLayoutHelper extends LayoutHelper {


	private PmService pmService = ServiceFactory.getInstance().getPmService();
	private LocationService locationService = ServiceFactory.getInstance().getLocationService(context);
	private CityItem cityItem;


	public SettingsLayoutHelper(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void initialize(View view) {
		// TODO Auto-generated method stub
		Button logout = null;
		logout = (Button) view.findViewById(R.id.logout);

		logout.setOnTouchListener(new OnTouchListener(){     
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					((Button) v).setTextColor(context.getResources().getColor(R.color.white));
				} else {
					((Button) v).setTextColor(context.getResources().getColor(R.color.black));
					AccessTokenKeeper.clear(context);
					CookieSyncManager.createInstance(context.getApplicationContext());  
					CookieSyncManager.getInstance().startSync();  
					CookieManager.getInstance().removeAllCookie();
					android.os.Process.killProcess(android.os.Process.myPid());
				}
				return false;
			}
		});

		Button button_share = (Button) view.findViewById(R.id.button_pm_share);
		button_share.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent();
				intent.setClass(context, PostActivity.class);
				Bundle bundle = new Bundle();
				StringBuffer sb = new StringBuffer();
				sb.append("我在");
				sb.append(cityItem.getName());
				sb.append(",当前的空气质量指数是");
				sb.append(cityItem.getAqiValue());
				sb.append(",空气质量");
				sb.append(cityItem.getQuality());
				sb.append(".");
				sb.append("#weieee# #空气状况# #api指数#");
				bundle.putSerializable("text",sb.toString());
				intent.putExtras(bundle);
				context.startActivity(intent);
			}
		});

		refresh_data(view);
	}

	private void refresh_data(final View v) {
		String currentCity = "南京";//locationService.getCurrentCity();
		pmService.getCityByName(currentCity, new UpdateTask() {
			@Override
			public void onUpdate(CityItem item) {
				cityItem = item;

				TextView city_name = (TextView) v.findViewById(R.id.textView_city);
				TextView aqi_value = (TextView) v.findViewById(R.id.textView_api_value);
				TextView air_quality = (TextView) v.findViewById(R.id.textView_air_quality);
				TextView update_time = (TextView) v.findViewById(R.id.textView_last_update);
				TextView O3_value = (TextView) v.findViewById(R.id.textView_O3);
				TextView SO2_value = (TextView) v.findViewById(R.id.textView_SO2);
				TextView NO2_value = (TextView) v.findViewById(R.id.textView_NO2);
				TextView PM25_value = (TextView) v.findViewById(R.id.textView_PM25);

				city_name.setText(item.getName());
				update_time.setText(item.getUpdateTime());
				aqi_value.setText(""+item.getAqiValue());
				air_quality.setText(item.getQuality());

				O3_value.setText(item.getPollutionValue("O3"));
				NO2_value.setText(item.getPollutionValue("NO2"));
				SO2_value.setText(item.getPollutionValue("SO2"));
				PM25_value.setText(item.getPollutionValue("PM2_5"));
			}
			@Override
			public void onErr(String msg) {
				Toast.makeText(context, msg, Toast.LENGTH_SHORT)
						.show();
			}
			@Override
			public void onFinal() {
				stopLoading(v);
			}
		});
	}

	private void stopLoading(View v){
		View l = v.findViewById(R.id.pm_layout);
		l.setVisibility(View.VISIBLE);
		View bar = v.findViewById(R.id.progress_pm);
		bar.setVisibility(View.GONE);
		Button button_share = (Button) v.findViewById(R.id.button_pm_share);
		button_share.setVisibility(View.VISIBLE);
	}

}
